package com.wu.springboot_study.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wu.springboot_study.annotion.OnlyCallOnce;
import com.wu.springboot_study.entity.User;
import com.wu.springboot_study.service.IUserService;
import com.wu.springboot_study.util.JedisUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author benjamin_5
 * @Description 用户接口
 * @date 2022/12/5
 */
@RestController
@RequestMapping("user")
@AllArgsConstructor
public class UserRestController {

    private final IUserService userService;

    private final JedisUtil jedisUtil;

    @GetMapping("setCache")
    public String setCache(String key, String value){
        jedisUtil.set(key,value);
        return key + ":" + jedisUtil.get(key);
    }

    /**
     *
     * @param name
     * @param age
     * @return
     */
    @GetMapping("list")
    @OnlyCallOnce
    public List<User> list(String name, Integer age){
        System.out.println("方法执行");
        return this.userService.list(name, age);
    }

    /**
     * 用户列表查询
     * @param name 姓名
     * @param age 年龄
     * @return 用户数据
     */
    @GetMapping("list2")
    public List<User> list2(String name, Integer age){
//        return userService.list2(name, age);
        return userService.list(Wrappers.<User>lambdaQuery().like(!StringUtils.isBlank(name), User::getName, name).eq(age != null, User::getAge, age));
    }

    /**
     * 分页查询
     * @param name
     * @param age
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("page")
    public Page<User> page(String name, Integer age, int page, int limit){
        Page<User> userPage = new Page<>(page,limit);
        LambdaQueryWrapper<User> eq = Wrappers.<User>lambdaQuery().like(!StringUtils.isBlank(name), User::getName, name).eq(age != null, User::getAge, age);
        return userService.page(userPage,eq);
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("delete")
    public Boolean delete(Long id){
        return userService.removeById(id);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PostMapping("save")
    public Boolean save(@RequestBody User user){
//        if(userService.getUserByName(user.getName()) != null){
        if(userService.isExistByName(user.getName())){
            System.out.println("用户名已存在");
            return false;
        }
        return userService.save(user);
    }

    @GetMapping("test")
    @OnlyCallOnce
    public String test(){
        System.out.println("方法调用开始");
        User user = userService.getById(1);
        Integer amount = user.getAmount();
        System.out.println("金额："+amount);
        if(amount < 100){
            System.out.println("钱不够");
            return "钱不够";
        }
        try {
            // 模拟业务操作耗时
            Thread.sleep(5000);
            // 扣钱
            user.setAmount(amount-100);
            userService.updateById(user);
            System.out.println("方法调用结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "方法调用结束";
    }

    @GetMapping("test1")
    public String test1(){
        System.out.println(jedisUtil.setnx("user1"));
        System.out.println(jedisUtil.setnx("user1"));
        System.out.println(jedisUtil.setnx("user1"));
        System.out.println(jedisUtil.setnx("user1"));
        return "方法调用结束";
    }

}
